package com.paradise.ytj.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paradise.ytj.R;
import com.paradise.ytj.utils.DialogUtils;

/**
 * Created by Administrator on 2017/11/7 0007.
 */

public class CustomDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private int widthPixels;
    private int heightPixels;
    private TextView tvTitle;
    private TextView cancel;
    private TextView submit;
    private View view;
    private LinearLayout llContent;
    private TextView tvDeclare;

    public CustomDialog(Context context) {
        this(context, 0);
        this.setCancelable(false);
        this.context = context;
        init();
    }


    public CustomDialog(Context context, int theme) {
        super(context, R.style.ShareDialog);
        this.context = context;
        init();
    }

    private void init() {
        widthPixels = context.getResources().getDisplayMetrics().widthPixels;
        heightPixels = context.getResources().getDisplayMetrics().heightPixels;
        view = View.inflate(context, R.layout.dialog, null);
        tvTitle = (TextView) view.findViewById(R.id.title);
        tvDeclare = (TextView) view.findViewById(R.id.tv_declare);
        llContent = (LinearLayout) view.findViewById(R.id.ll_content);
        cancel = (TextView) view.findViewById(R.id.cancel);
        submit = (TextView) view.findViewById(R.id.submit);
        // this.show();
        Window window = this.getWindow();
        window.setContentView(view);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = 4 * widthPixels / 10;
        params.height = 4 * heightPixels / 10;
        params.alpha = 1.0f;
        window.setAttributes(params);
        this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        cancel.setOnClickListener(this);
        submit.setOnClickListener(this);
    }

    public void setTitleText(String text) {
        tvTitle.setText(text);
    }

    public void setTitleText(int id) {
        tvTitle.setText(id);
    }

    public void setDeclareText(String text) {
        tvDeclare.setVisibility(View.VISIBLE);
        tvDeclare.setText(text);
    }

    public void setDeclareText(int id) {
        tvDeclare.setVisibility(View.VISIBLE);
        tvDeclare.setText(id);
    }

    public void setCancelText(String text) {
        cancel.setText(text);
    }

    public void setSubmitText(String text) {
        submit.setText(text);
    }

    public LinearLayout getContentView() {
        return llContent;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                onCancelListener.onCancelClick();
                break;
            case R.id.submit:
                onSubmitListener.onSubmitClick();
                break;
        }
    }

    private OnCancelListener onCancelListener;
    private OnSubmitListener onSubmitListener;

    public interface OnCancelListener {
        void onCancelClick();
    }

    public interface OnSubmitListener {
        void onSubmitClick();
    }

    public void setOnCancelListener(OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
    }

    public void setOnSubmitListener(OnSubmitListener onSubmitListener) {
        this.onSubmitListener = onSubmitListener;
    }

    public void show() {
        super.show();
        DialogUtils.addDialog(this);
    }

    public void dismiss() {
        super.dismiss();
        DialogUtils.removeDialog(this);
    }
}
